package com.example.bes.springboot.web.war.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.Query;
import java.lang.management.ManagementFactory;
import java.util.Set;

@RestController
public class TestController {

    private static final Logger log = LoggerFactory.getLogger(TestController.class);

    @GetMapping(value = "/test")
    public String test() throws Exception {
        //System.out.println(name);
        log.info("welcome to use BES");
        log.info(("中文乱码测试"));
        return "welcome to use BES";
    }

    @GetMapping(value = "/haha")
    public String haha() {
        return "haha";
    }

    @GetMapping(value = "/port")
    public Set getPort() throws MalformedObjectNameException {
        MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
        Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"), Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
        return objectNames;
    }

    @PutMapping(value = "/testput")
    public String testPutMethod() {
        log.info("===== test put method =====");
        return "test put method";
    }

    @DeleteMapping(value = "/testdelete")
    public String testDeleteMethod() {
        log.info("===== test delete method =====");
        return "test delete method";
    }

    @RequestMapping(value = "/testtrace", method = RequestMethod.TRACE)
    public String testTraceMethod() {
        log.info("===== test trace method =====");
        return "test trace method";
    }

    @RequestMapping(value = "/testoptions", method = RequestMethod.OPTIONS)
    public String testOptionsMethod() {
        log.info("===== test options method =====");
        return "test options method";
    }
}
